<template>
  <transition name="slide">
    <music-list :songs="songs" :bg-img="bgImg" :title="title"></music-list>
  </transition>
</template>
<script>
import MusicList from '../music-list/music-list'
import {getSingerDetail} from '@/api/singer'
import {mapGetters} from 'vuex'
import {ERR_OK} from '@/api/config'
import {creatSong, isValidMusic, processSongsUrl} from '@/common/js/song'
export default {
  components: {
    MusicList
  },
  props: {},
  data() {
    return {
      songs: []
    }
  },
  watch: {},
  computed: {
    ...mapGetters(['singer']),
    bgImg() {
      return this.singer.avatar
    },
    title() {
      return this.singer.name
    }
  },
  methods: {
    _getDetail() {
      if (!this.singer.id) {
        // 边界处理 当用户在详情页面不小心刷新了页面  直接让他回退到歌手页面
        this.$router.push('/singer')
        return
      }
      getSingerDetail(this.singer.id).then((res) => {
        if (res.code === ERR_OK) {
          processSongsUrl(this._normalLizeSongs(res.data.list)).then((songs) => {
            this.songs = songs
          })
        }
      })
    },
    _normalLizeSongs(list) {
      let ret = []
      list.forEach((item) => {
        let {musicData} = item
        if (isValidMusic(musicData)) {
          ret.push(creatSong(musicData))
        }
      })
      return ret
    }
  },
  created() {
    this._getDetail()
    console.log(this.singer)
  },
  mounted() {}
}
</script>
<style lang="stylus" scoped>
.slide-enter-active,.slide-leave-active{
  transition: all 0.3s
}
.slide-enter,.slide-leave-to{
  transform: translate3d(100%, 0, 0)
}
</style>